package com.dajingling.wx.Controller;

import com.dajingling.bean.*;
import com.dajingling.mvc.ResponseBody;
import com.dajingling.service.EUserExpressService;
import com.dajingling.service.ExpressService;
import com.dajingling.service.UserAdminService;
import com.dajingling.util.DateFormatUtil;
import com.dajingling.util.JSONUtil;
import com.dajingling.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        UserAdmin wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if(list.size()==0){
            msg.setStatus(-1);
        }else{
            msg.setStatus(0);
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int)(o1Time-o2Time);
            });
            Object[] s0 = status0Express.toArray();
            Object[] s1 = status1Express.toArray();
            Map data = new HashMap<>();
            data.put("status0",s0);
            data.put("status1",s1);
            msg.setData(data);
        }
        String json = JSONUtil.toJSON(msg.getData());
        System.out.println("微信查询快递："+json);
        return json;
    }

    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        List<Express> list = ExpressService.findByUserPhone(userPhone);
        List<BootStrapTableExpress> list2 = new ArrayList<>();
        for(Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            list2.add(e2);
        }
        Message msg = new Message();
        if(list.size() == 0){
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list2);
        }
        String json =  JSONUtil.toJSON(msg);
        System.out.println("微信查询快递："+json);
        return json;
    }

    @ResponseBody("/user_home.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = UserUtil.getWxUser(request.getSession()).getUserPhone();
        Message msg = new Message();
        EUserExpress eUserExpress = EUserExpressService.findByUserPhone(userPhone);
        UserAdmin userAdmin = UserAdminService.findByUserPhone(userPhone);
        System.out.println("返回用户名中...");
        String username = "未定义的用户";
        if(eUserExpress!=null){
            //快递员
            username = eUserExpress.getUserName();
            msg.setResult("快递员");
        }else if(userAdmin!=null){
                //用户
            username = userAdmin.getUserName();
            msg.setResult("用户");
        }
        msg.setData(username);
        String json = JSONUtil.toJSON(msg);
        System.out.println("退出登录："+json);
        return json;
    }

    @ResponseBody("/wx/findExpressByNumber.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        BootStrapTableExpress e2 = null;
        if(e!=null){
            String inTime = DateFormatUtil.format(e.getInTime());
            e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(),
                    e.getUserPhone(), e.getCompany(), e.getCode()==null?"已取出":e.getCode(),
                    inTime, e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()),e.getStatus()==0?"待取件":"已取件", e.getSysPhone());
        }
        Message msg = new Message();
        if(e == null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e2);
        }
        String json = JSONUtil.toJSON(msg);
        System.out.println("主页取件查询："+json);
        return json;
    }

    @ResponseBody("/wx/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        String sysPhone = UserUtil.getWxUser(request.getSession()).getUserPhone();
        List<Express> list = ExpressService.findBySysPhone(sysPhone);
        List<BootStrapTableExpress> list1 = new ArrayList<>();
        for (Express e:list){
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), e.getCode(), inTime, outTime, status, e.getSysPhone());
            list1.add(e2);
        }
        int total = list1.size();
        //4.把集合封装为bootstrap-table(前端的表格)能识别的格式
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        System.out.println("快递列表："+json);
        return json;
    }


}
